package com.sds.smarthome.foundation.repository.remote;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sds.smarthome.foundation.BaiduOpenApi;
import com.sds.smarthome.foundation.SHCloudApi;
import com.sds.smarthome.foundation.SHLogger;
import com.sds.smarthome.foundation.TulingOpenApi;
import com.sds.smarthome.foundation.util.XLog;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by L on 2016/5/17.
 */
public final class RetrofitHttpBuilder {

    private static final Gson GSON = new GsonBuilder().create();

    public static SHCloudApi buildUserApi(String baseUrl, Interceptor interceptor) {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                XLog.f(message);
            }
        });
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logInterceptor)
                .addInterceptor(interceptor)
                .connectTimeout(3, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .sslSocketFactory(getSSLFatory())
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(GSON))
                .build();

        return retrofit.create(SHCloudApi.class);
    }
    public static SHLogger buildCloudLogApi(String baseUrl) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(3, TimeUnit.SECONDS)
                .readTimeout(2, TimeUnit.SECONDS)
                .sslSocketFactory(getSSLFatory())
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(GSON))
                .build();

        return retrofit.create(SHLogger.class);
    }
    public static BaiduOpenApi buildBaiduOpenApi(String baseUrl) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(12, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(GSON))
                .build();

        return retrofit.create(BaiduOpenApi.class);
    }

    public static TulingOpenApi buildTulingApi(String baseUrl) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(12, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(GSON))
                .build();

        return retrofit.create(TulingOpenApi.class);
    }

    private static SSLSocketFactory getSSLFatory() {

        try {
            final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[]{};
                }
            }};
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new SecureRandom());
            return sc.getSocketFactory();
        } catch (Exception e) {
            XLog.e("ssl  "+e.getMessage());
        }
        return null;
    }
}
